﻿using System.Collections.Generic;
using System.Linq;

namespace CoreLims.Models.CoreLims
{
    using Analyte = SampleAnalyte;

    public static class SampleAnalyteExtension
    {
        public static List<Analyte> getAnalyteList(this SampleAnalyte analyte, CoreLimsContext dbContext) => dbContext.Analyte.ToList();

        /// <summary>
        /// 根据 分类 获取分析项数据
        /// </summary>
        /// <param name="Classification"></param>
        /// <returns></returns>
        public static List<Analyte> getAnalyteListByClassification(this SampleAnalyte analyte, CoreLimsContext dbContext, string Classification) => 
            dbContext.Analyte.Where(s => s.Classification == Classification).OrderBy(x => x.Code).ToList();
        
        public static Analyte getAnalyte(this SampleAnalyte analyte, CoreLimsContext dbContext, string Id) => dbContext.Analyte.Where(m => m.Id == Id).FirstOrDefault();

        public static string AddAnalyte(this SampleAnalyte analyte, CoreLimsContext dbContext)
        {
            analyte.Id = SAMPLE._newId();
            dbContext.Add(analyte);
            dbContext.SaveChanges();
            return "分析项添加成功！";
        }

        public static string UpdateAnalyte(this SampleAnalyte analyte, CoreLimsContext dbContext)
        {
            SampleAnalyte ana = SAMPLE.Analyte.getAnalyte(dbContext, analyte.Id);
            ana.Copy(analyte);
            dbContext.Update(ana);
            dbContext.SaveChanges();
            return "分析项信息更新成功！";
        }

        /// <summary>
        /// 更新报告过滤
        /// </summary>
        /// <param name="spec"></param>
        /// <param name="dbContext"></param>
        /// <param name="Id"></param>
        /// <param name="Enable"></param>
        /// <returns></returns>
        public static string UpdateAssist(this SampleAnalyte analyte, CoreLimsContext dbContext, string Id, string Assist)
        {
            var ana = SAMPLE.Analyte.getAnalyte(dbContext, Id);

            if (Assist == "true")
                ana.Assist = true;
            else
                ana.Assist = false;

            dbContext.Update(ana);
            dbContext.SaveChanges();
            return "状态更新成功！";
        }

        /// <summary>
        /// 更新 CMA 状态
        /// </summary>
        /// <param name="analyte"></param>
        /// <param name="dbContext"></param>
        /// <param name="Id"></param>
        /// <param name="CMA"></param>
        /// <returns></returns>
        public static string UpdateCMA(this SampleAnalyte analyte, CoreLimsContext dbContext, string Id, string CMA)
        {
            var ana = SAMPLE.Analyte.getAnalyte(dbContext, Id);

            if (CMA == "true")
                ana.CMA = true;
            else
                ana.CMA = false;

            dbContext.Update(ana);
            dbContext.SaveChanges();
            return "状态更新成功！";
        }

        /// <summary>
        /// 更新 CNAS 状态
        /// </summary>
        /// <param name="analyte"></param>
        /// <param name="dbContext"></param>
        /// <param name="Id"></param>
        /// <param name="CNAS"></param>
        /// <returns></returns>
        public static string UpdateCNAS(this SampleAnalyte analyte, CoreLimsContext dbContext, string Id, string CNAS)
        {
            var ana = SAMPLE.Analyte.getAnalyte(dbContext, Id);

            if (CNAS == "true")
                ana.CNAS = true;
            else
                ana.CNAS = false;

            dbContext.Update(ana);
            dbContext.SaveChanges();
            return "状态更新成功！";
        }

        /// <summary>
        /// 根据传入的 Id ，更新对应分析项的 默认指标ID 和 默认指标名称
        /// </summary>
        /// <param name="Id">分析项ID</param>
        /// <param name="DefaultSpecId">默认指标ID</param>
        /// <param name="DefaultSpecName">默认指标名称</param>
        /// <returns></returns>
        public static string UpdateAnalyte(this SampleAnalyte analyte, CoreLimsContext dbContext, string Id, string DefaultSpecId, string DefaultSpecName)
        {
            var ana = SAMPLE.Analyte.getAnalyte(dbContext, Id);
            ana.DefaultSpecId = DefaultSpecId;
            ana.DefaultSpecName = DefaultSpecName;
            dbContext.Update(ana);
            dbContext.SaveChanges();
            return "分析项信息更新成功！";
        }

        /// <summary>
        /// 删除分析项时，关联的附件会一起被删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static string DeleteAnalyte(this SampleAnalyte analyte, CoreLimsContext dbContext, string Id)
        {
            SampleAnalyte ana = SAMPLE.Analyte.getAnalyte(dbContext, Id);

            // 删除分析项时，需要检查指标集是否与该项有关联，如果有则不能删除。
            var specCollection = dbContext.Spec.Where(s => s.IsCollection == true && s.AnalyteId == ana.Id).FirstOrDefault();
            if (specCollection != null)
                return "该分析项与指标集 " + specCollection.Name + " 关联，若要删除该分析项，请先删除关联关系！";

            // 删除分析项时，需要检查测试关系表中是否包含该分析项定义，如果有则不能删除。
            var trl = dbContext.TestRelation.FirstOrDefault(s => s.AnalytesId == ana.Id);
            if (trl != null)
            {
                var test = SAMPLE.TestDefinition.getTestDefinition(dbContext, trl.TestDefinitionId);
                return "该分析项与测试 " + test.TestName + " 关联，若要删除该分析项，请先删除关联关系！";
            }

            // 删除分析项时，需要检查样品定义关系表，如果有引用则不能删除。
            var rl = dbContext.Relation.FirstOrDefault(s => s.IncludedId == ana.Id);
            if (rl != null)
            {
                var sample = SAMPLE.SampleDefinition.getSampleDefinition(dbContext, rl.SourceId);
                return "该分析项与样品定义 " + sample.Name + " 关联，若要删除该分析项，请先删除关联关系！";
            }

            // 删除分析项时，需要检查登录样品关系表中是否有该项的关联（不论是直接关联还是通过测试关联），有则不能删除。
            var name = SAMPLE.Sample.CheckHasAnalyte(dbContext, ana.Id);
            if (name != null)
                return "该分析项与登录样品 " + name + " 关联，若要删除该分析项，请开始或删除该样品！";

            Common.UploadFile.DeleteUploadFile(dbContext,"Analyte", ana.Id);
            dbContext.Remove(ana);
            dbContext.SaveChanges();
            return "删除成功！";
        }

        /// <summary>
        /// 复制记录内容，ID不复制
        /// </summary>
        /// <param name="supplier"></param>
        /// <param name="other"></param>
        /// <returns></returns>
        public static SampleAnalyte Copy(this SampleAnalyte analyte, SampleAnalyte other)
        {
            analyte.Code = other.Code;
            analyte.Classification = other.Classification;
            analyte.English = other.English;
            analyte.Name = other.Name;
            analyte.ResultModel = other.ResultModel;
            analyte.RoundingOff = other.RoundingOff;
            analyte.Units = other.Units;
            analyte.FUnits = other.FUnits;
            analyte.DefaultSpecId = other.DefaultSpecId;
            analyte.UnitsType = other.UnitsType;
            analyte.Description = other.Description;
            analyte.Assist = other.Assist;
            analyte.DetectionLimit = string.IsNullOrEmpty(other.DetectionLimit)?null: other.DetectionLimit;
            analyte.DefaultValue = string.IsNullOrEmpty(other.DefaultValue) ? null : other.DefaultValue;
            analyte.AlternativeResults = string.IsNullOrEmpty(other.AlternativeResults) ? null : other.AlternativeResults;
            analyte.MethodId = other.MethodId;
            analyte.MethodCode = other.MethodCode;
            analyte.CMA = other.CMA;
            analyte.CNAS = other.CNAS;
            return analyte;
        }

        /// <summary>
        /// 更新 设备类型列表
        /// </summary>
        /// <param name="spdf"></param>
        /// <param name="dbContext"></param>
        /// <param name="Id"></param>
        /// <param name="EquipmentTypeList"></param>
        /// <returns></returns>
        //public static string UpdateAnalyteEquipmentTypeList(this SampleAnalyte spdf, CoreLimsContext dbContext, string Id, string EquipmentTypeList)
        //{
        //    Analyte sd = SAMPLE.Analyte.getAnalyte(dbContext, Id);
        //    sd.EquipmentTypeList = EquipmentTypeList;

        //    dbContext.Update(sd);
        //    dbContext.SaveChanges();
        //    return "设备类型列表更新成功！";
        //}

    }

}
